package org.lonetree.tc.webapp.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.lonetree.tc.core.Course;
import org.lonetree.tc.core.Course.CourseId;
import org.lonetree.tc.core.dao.CourseDAO;
import org.lonetree.tc.core.dao.DAOFactory;
import org.lonetree.tc.webapp.forms.CourseForm;

public class CreateSection extends AdminAction {

	@Override
	protected ActionForward executeAdmin(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		try {
			CourseForm cf = (CourseForm) form;
			CourseId cid = new CourseId(cf.getId());
			DAOFactory factory = DAOFactory.getDAOFactory(DAOFactory.FactoryImplementation.DB_DAO_FACTORY);
			CourseDAO cdao = factory.getCourseDAO();
			
			cdao.createCourse(cid);
			Course course = cdao.getCourse(cid);
			course.setTitle(cf.getCourse_title());
			

			//String[] authors=cf.getAuthor().split(",");
			//book.setAuthor(authors);
			
			request.setAttribute("section.add.message", "Section added successfully");
			return mapping.findForward("success");
		} catch (Exception e) {
			request.setAttribute("section.add.message", "Section failed to add");
			return mapping.findForward("failure");
		}
	}

}
